package com.wlf.html.controller;

import com.wlf.html.model.User;
import com.wlf.html.service.UserService;

import net.sf.json.JSONObject;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserController {

	JSONObject jsonObject1 = new JSONObject();
	
    @Autowired
    UserService userService;

    /* 添加用户 */
    @RequestMapping(value = "/api/v1/user/addUserInfo",produces = {"application/json;charset=UTF-8"})
    public String addUserInfo(HttpServletRequest request, HttpServletResponse response,User user) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        int i = 0;
        String username = user.getUsername();
        i = userService.checkedUsername(username);
        if(i > 0) {
            System.out.println("faile,用户存在,注册失败");
            return "html/failed";
        }else {
            boolean b = userService.addUserInfo(user);
            if(b) {
                System.out.println("ok,注册成功");
                return "html/register_success";
            }else {
                System.out.println("failed,注册失败");
                return "html/failed";
            }
        }
    }
    
    /* 登录账号 */
	@RequestMapping(value = "/api/v1/user/loginUserInfo",produces = {"application/json;charset=UTF-8"})
	public String loginUserInfo(HttpServletRequest request, HttpServletResponse response,@Param("username") String username, @Param("password") String password) throws Exception {
		request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String password1 = request.getParameter("password1");
		User list = userService.loginUserInfo(username, password);
		if(list != null) {
			request.getSession().setAttribute("username",list.getUsername());
			if(password1.equals(password)) {
				System.out.println(list);
				return "html/success";
			}else {
				return "html/failed";
			}
		}else {
			return "html/failed";
		}
	}
	
	@RequestMapping(value = "/user/addUserInfo",produces = {"application/json;charset=UTF-8"})
    public String addUserHTInfo(HttpServletRequest request, HttpServletResponse response,@Param("username") String username, @Param("password") String password,  @Param("type") int type, @Param("create_time") String create_time) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setType(type);
        user.setCreateTime(create_time);
        int i = 0;
        i = userService.checkedUsername(username);
        if(i > 0) {
            System.out.println("faile,用户名存在,注册失败");
            return "admin/failed";
        }else {
            boolean b = userService.addUserInfo(user);
            if(b) {
                System.out.println("ok,注册成功");
                return "admin/register_success";
            }else {
                System.out.println("failed,注册失败");
                return "admin/failed";
            }
        }
    }
    
    /* 登录账号 */
	@RequestMapping(value = "/user/loginUserInfo",produces = {"application/json;charset=UTF-8"})
	public String loginUserHTInfo(HttpServletRequest request, HttpServletResponse response,@Param("username") String username, @Param("password") String password) throws Exception {
		request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        User list = userService.loginUserInfo(username, password);
		if(list != null) {
			System.out.println(list);
			if(list.getType() != 1) {
				request.getSession().setAttribute("type",list.getType());
				return "admin/failed";
			}
			request.getSession().setAttribute("username",list.getUsername());
			//获取当前系统时间
			Date date = new Date();
	        String ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	        request.getSession().setAttribute("loadtime", ft);
			return "admin/success";
		}else {
			return "admin/failed";
		}
	}
	
	/* 查询所有数据 */
    @RequestMapping(value = "/user/findAll",produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)
    public @ResponseBody String findAll(HttpServletRequest request, HttpServletResponse response,@Param("page") String page, @Param("limit") String limit) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        List<User> list = userService.findAll();
        jsonObject1.put("userlist", list);
        System.out.println(jsonObject1.toString());
    	return jsonObject1.toString();
    }
    
    /* 查询所有数据 */
    @RequestMapping(value = "/user/findUserInfo",produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)
    public @ResponseBody String findAll(HttpServletRequest request, HttpServletResponse response,@Param("page") String page, @Param("limit") String limit, @Param("user_id") String user_id, @Param("username") String username,@Param("phone") String phone) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        List<User> list = userService.findUserInfo(user_id,username,phone);
        jsonObject1.put("userlist", list);
        System.out.println(jsonObject1.toString());
    	return jsonObject1.toString();
    }
	
}
